import Cookies from 'js-cookie'

const state = {
  sidebar: {
    opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    withoutAnimation: false
  },
  device: 'desktop',
  router: null,
  menuNoList: [],
  codeList: [],
  title:'',
  WarrantyInfo:{}
}

function setDiaLogWidth(sidebarStatus) {
  const bodyDom = document.getElementsByTagName("body")
  if (bodyDom && bodyDom.length > 0) {
    bodyDom[0].className = ''
    if (!sidebarStatus) {
      bodyDom[0].className += 'hasSidebar'
    }
    console.log(sidebarStatus)
  }
}
const mutations = {
  TOGGLE_SIDEBAR: state => {
    console.log('哈哈哈哈哈哈哈哈')
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
    setDiaLogWidth(state.sidebar.opened ? 1 : 0)
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    console.log('哈哈哈哈哈哈哈哈')
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
    setDiaLogWidth(state.sidebar.opened ? 1 : 0)
  },

  WARRANTY_STATUS: (state, onInfo) => {
    // console.log("WARRANTY_STATUS", onInfo);
    Cookies.set("WarrantyInfo", encodeURIComponent(JSON.stringify(onInfo)));
    state.WarrantyInfo = onInfo;
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  SET_ROUTER: (state, router) => {
    state.router = router
  },
  SET_MENUNOLIST: (state, menuNoList) => {
    state.menuNoList = menuNoList
  },
  SET_CODELIST: (state, list) => {
    console.log(list)
    state.codeList = list
  },
  SET_TITLE: (state, title) => {
    console.log(title, '-------------------------')
    state.title = title
  },
  WARRANTY_STATUS: (state, onInfo) => {
    // console.log("WARRANTY_STATUS", onInfo);
    Cookies.set("WarrantyInfo", encodeURIComponent(JSON.stringify(onInfo)));
    state.WarrantyInfo = onInfo;
  },
}

const actions = {
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  warrantyStatus({ commit }, onInfo) {
    // console.log("Warranty Status);", state, commit);
    if (JSON.stringify(state.WarrantyInfo) !== JSON.stringify(onInfo)) {
      // console.log(typeof state.WarrantyInfo , typeof onInfo)
      commit("WARRANTY_STATUS", onInfo);
    }
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  warrantyStatus({ commit }, onInfo) {
    // console.log("Warranty Status);", state, commit);
    if (JSON.stringify(state.WarrantyInfo) !== JSON.stringify(onInfo)) {
      // console.log(typeof state.WarrantyInfo , typeof onInfo)
      commit("WARRANTY_STATUS", onInfo);
    }
  },
  saveRouter({ commit }, router) {
    console.log('存储saveRouter：', router)
    commit('SET_ROUTER', router)
  },
  saveMenuList({ commit }, router) {
    console.log('存储saveRouter：', router)
    commit('SET_MENUNOLIST', router)
  },
  setCodeList({ commit }, list) {
    console.log('存储CodeList：', list)
    commit('SET_CODELIST', list)
  },
  setTitle({ commit }, systemFlag) {
    let title = ''
    if (systemFlag == 'SC') {
      // title = '生产系统'
      title = '养殖生产管理系统'
    } else if (systemFlag == 'YY') {
      title = '运营系统'
    } else {
      title = '水产养殖系统v3.1'
    }
    commit('SET_TITLE', title)
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
